var mqtt = require('../../utils/mqtt.min');
//一个全局变量...
var client = null;


const app = getApp();
Page({
  data: {
    StatusBar: app.globalData.StatusBar,
    CustomBar: app.globalData.CustomBar,
    index: null,
    multiIndex: [0, 0, 0],
    startTimeChange: '20:00',
    forksCount: '8:00',
    starCount: null,
    visitTotal: '大郎该吃药了',
  },
  startTimeChange(e) {
    this.setData({
      startTimeChange: e.detail.value
    })
  },
  endTimeChange(e) {
    this.setData({
      endTimeChange: e.detail.value
    })
  },
  DateChange(e) {
    this.setData({
      date: e.detail.value
    })
  },
  RegionChange: function(e) {
    this.setData({
      region: e.detail.value
    })
  },

  checkSetTimeChange(e) {
    var that = this
    
    console.log(e.detail.value)
    //对输入的字符需要校验
    //弹窗
    //wx.showToast({
      //title: "请阅读并同意用户须知",
     // icon: 'none'
    //})

  },

  formSubmit:function(e){
    console.log(e)

  },


  connected:function() {
    const options = {
      connectTimeout: 4000, // 超时时间
      clientId: 'wx_' + parseInt(Math.random() * 100 + 800, 10),
      port: 443,  //重点注意这个,坑了我很久
    }
    client = mqtt.connect('wxs://broker.hivemq.com', options)
    client.on('reconnect', (error) => {
        console.log('正在重连:', error)
    })

    client.on('error', (error) => {
        console.log('连接失败:', error)
    })

    let that = this;
    client.on('connect', (e) => {
      wx.showToast({
        title: '成功连接服务器',
        icon: 'none'
      })
        console.log('成功连接服务器')
　　　　　　　//订阅一个主题
        client.subscribe('message.queue', {
            qos: 0
        }, function(err) {
            if (!err) {
                console.log("订阅成功")
            }
        })
    })
    client.on('message', function (topic, message) {
        console.log('received msg:' + message.toString());
    })
  }
})